//
//  WKUserBooksListCell.m
//  WKMaybell
//
//  Created by 何文虎 on 2018/12/25.
//  Copyright © 2018 何文虎. All rights reserved.
//

#import "WKUserBooksListCell.h"
#import "WKContenCellPhotoView.h"
#import "WKContenCellViedeoView.h"

@interface WKUserBooksListCell ()
@property(nonatomic,strong)UIImageView *timeIconImgView;
@property(nonatomic,strong)UILabel *timeAndcountLb;
@property(nonatomic,strong)UIImageView *line;
@property(nonatomic,strong)UIView *bgView;
@property(nonatomic,strong)UILabel *titleLb;
@property(nonatomic,strong)UILabel *contentLb;
@property(nonatomic,strong)UIView *zyView;
@property(nonatomic,strong)UIButton *zanBtn;
@property(nonatomic,strong)UIButton *commentBtn;
@end

@implementation WKUserBooksListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setup];
    }
    return self;
}

- (void)setup{
    
    
    CGFloat toleft = 32;
    
    [self.contentView addSubview:self.timeIconImgView];
    [self.timeIconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.top.mas_equalTo(15);
        make.size.mas_equalTo(CGSizeMake(13, 13));
    }];
    
    [self.contentView addSubview:self.timeAndcountLb];
    [self.timeAndcountLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(32);
        make.right.mas_equalTo(-12);
        make.top.mas_equalTo(15);
        make.height.mas_equalTo(13);
    }];
    
    [self.contentView addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(toleft);
        make.right.mas_equalTo(-12);
        make.top.mas_equalTo(self.timeIconImgView.mas_bottom).offset(10);
//        make.height.mas_equalTo(600);
    }];
    
    [self.bgView addSubview:self.titleLb];
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.right.mas_equalTo(-12);
        make.top.mas_equalTo(15);
        make.height.mas_equalTo(15);
    }];
    
    [self.bgView addSubview:self.contentLb];
    [self.contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.right.mas_equalTo(-12);
        make.top.mas_equalTo(self.titleLb.mas_bottom).offset(12);
        make.height.mas_equalTo(30);
    }];
    
    [self.bgView addSubview:self.zyView];
    [self.zyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.right.mas_equalTo(-12);
        make.top.mas_equalTo(self.contentLb.mas_bottom).offset(12);
    }];
    
     CGFloat zyWidth = ScreenWidth - 24 - toleft;
    
    self.contentLb.numberOfLines = 0;
    self.contentLb.preferredMaxLayoutWidth = ScreenWidth - zyWidth -24;
    [self.contentLb sizeToFit];
    
//    CGFloat zyWidth = ScreenWidth - 12 - toleft;
    CGFloat btnWidth = 55;
    CGFloat btnHeight = 45;
    CGFloat half_width = zyWidth/2;
    CGFloat btnx = (half_width-btnWidth)/2;
    
    [self.bgView addSubview:self.zanBtn];
    [self.zanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(btnx);
        make.size.mas_equalTo(CGSizeMake(btnWidth, btnHeight));
        make.top.mas_equalTo(self.zyView.mas_bottom);
    }];
    
    [self.bgView addSubview:self.commentBtn];
    [self.commentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-btnx);
        make.size.mas_equalTo(CGSizeMake(btnWidth, btnHeight));
        make.top.mas_equalTo(self.zanBtn.mas_top);
    }];
    
    
    UIView *pointview = [[UIView alloc]init];
    [self.contentView addSubview:pointview];
    [pointview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bgView.mas_top);
        make.size.mas_equalTo(CGSizeMake(5, 5));
        make.centerX.mas_equalTo(self.timeIconImgView.mas_centerX);
    }];
    pointview.backgroundColor = UIColorFromHex(0x979797);
    pointview.layer.cornerRadius = 5/2;
    pointview.layer.masksToBounds = YES;
    
    [self.contentView addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(pointview.mas_bottom);
        make.width.mas_equalTo(1);
        make.centerX.mas_equalTo(pointview.mas_centerX);
        make.bottom.mas_equalTo(self.bgView.mas_bottom);
    }];
    _line.backgroundColor = UIColorFromHex(0x979797);
    
    _timeIconImgView.backgroundColor = BACK_COLOR;
    _timeIconImgView.layer.masksToBounds = YES;
    _timeIconImgView.layer.cornerRadius  = 13/2;
    
    _zanBtn.backgroundColor = [UIColor greenColor];
    _commentBtn.backgroundColor = [UIColor magentaColor];
    
    _bgView.layer.masksToBounds = YES;
    _bgView.layer.cornerRadius = 5;
    _bgView.layer.borderWidth = 1;
    _bgView.layer.borderColor = LINEBACK_COLOR.CGColor;
    
    self.hyb_bottomOffsetToCell = 15;
    self.hyb_lastViewInCell = self.bgView;
//    self.hyb_lastViewsInCell = @[self.bgView,self.zanBtn,self.commentBtn];
    
    
}

- (void)displayUserBooksListCellWithModel:(WKUserBooksListModel*)listModel{
    
    
    NSString *time = BLANKSTRING(listModel.time);
    NSString *count = [NSString stringWithFormat:@"第%ld篇日记",listModel.count];
    NSString *str = [NSString stringWithFormat:@"%@ %@",time,count];
    
    [WHAttributedString setRichTextOnlyColor:_timeAndcountLb titleString:str textColor:BACK_COLOR colorRang:NSMakeRange(0, time.length)];
    _titleLb.text = BLANKSTRING(listModel.title);
    _contentLb.text = BLANKSTRING(listModel.content);
    
    CGFloat lb_height = [listModel.content boundingRectWithSize:CGSizeMake(ScreenWidth-68, MAXFLOAT) font:PFSC_RegularFont(12) lineSpacing:5].height;
    CGFloat max_height = _contentLb.font.lineHeight * 2;
    CGFloat content_height = lb_height > max_height ? max_height : lb_height;
    
    [_contentLb mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(content_height);
    }];
    
    
    NSString *zanstr = [NSString stringWithFormat:@"%ld",listModel.zanNum];
    [_zanBtn setTitle:zanstr forState:UIControlStateNormal];
    
    NSString *commetstr = [NSString stringWithFormat:@"%ld",listModel.commentNum];
    [_commentBtn setTitle:commetstr forState:UIControlStateNormal];
    
    
    [_zyView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    CGFloat bg_height = 103 + content_height;
    
    //图文
    if (listModel.bookType == 1) {
       
        if (listModel.imgs.count) {
            WKContenCellPhotoView *photoView = [WKContenCellPhotoView new];
            [_zyView addSubview:photoView];
            [photoView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.left.right.width.mas_equalTo(0);
            }];
            
            photoView.picPathStringsArray = listModel.imgs;
            CGFloat height = photoView.height;
            
            [_zyView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(height);
            }];
            
            bg_height+=height;
        }
    }
    
    //视频
    if (listModel.bookType == 2) {
            WKContenCellViedeoView *videoView = [[WKContenCellViedeoView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth-68, 153)];
            [_zyView addSubview:videoView];
            videoView.playVideoBlock = ^{
//                if (self.playVideoBlock) {
//                    self.playVideoBlock();
//                }
            };
            [_zyView mas_updateConstraints:^(MASConstraintMaker *make) {
    
                make.height.mas_equalTo(153);
            }];
         bg_height+=153;
    }
    
    [self.bgView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(bg_height);
    }];
    
}

- (UIImageView*)timeIconImgView{
    if (!_timeIconImgView) {
        _timeIconImgView = [[UIImageView alloc]init];
    }
    return _timeIconImgView;
}

- (UILabel*)timeAndcountLb{
    if (!_timeAndcountLb) {
        _timeAndcountLb = [[UILabel alloc]init];
        _timeAndcountLb.textColor = UIColorFromHex(0x979797);
        _timeAndcountLb.font = PFSC_RegularFont(11);
    }
    return _timeAndcountLb;
}

- (UIImageView*)line{
    if (!_line) {
        _line = [[UIImageView alloc]init];
    }
    return _line;
}

- (UIView*)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc]init];
    }
    return _bgView;
}

- (UILabel*)titleLb{
    if (!_titleLb) {
        _titleLb = [[UILabel alloc]init];
        _titleLb.textColor = UIColorFromHex(0x3C3C3C);
        _titleLb.font = PFSC_RegularFont(15);
    }
    return _titleLb;
}

- (UILabel*)contentLb{
    if (!_contentLb) {
        _contentLb = [[UILabel alloc]init];
        _contentLb.textColor = UIColorFromHex(0x9797997);
        _contentLb.font = PFSC_RegularFont(12);
    }
    return _contentLb;
}

- (UIView*)zyView{
    if (!_zyView) {
        _zyView = [[UIView alloc]init];
    }
    return _zyView;
}

- (UIButton*)zanBtn{
    if (!_zanBtn) {
        _zanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _zanBtn.titleLabel.font = PFSC_MediumFont(11);
        [_zanBtn setTitleColor:UIColorFromHex(0x979797) forState:UIControlStateNormal];
        [_zanBtn setTitleColor:BACK_COLOR forState:UIControlStateSelected];
        [_zanBtn setImage:ImageNamed(@"zan_normal") forState:UIControlStateNormal];
        [_zanBtn setImage:ImageNamed(@"zan_selected") forState:UIControlStateSelected];
    }
    return _zanBtn;
}

- (UIButton*)commentBtn{
    if (!_commentBtn) {
        _commentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _commentBtn.titleLabel.font = PFSC_MediumFont(11);
        [_commentBtn setTitleColor:UIColorFromHex(0x979797) forState:UIControlStateNormal];
        [_commentBtn setImage:ImageNamed(@"comment_normal") forState:UIControlStateNormal];
    }
    return _commentBtn;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
